#!/bin/bash

# 2007-2018 by Paul Sherman
# for Absolute Linux.
# tests tgz and txz files to check if they are Slackware install packages,
# and sends thenm off to "installpkg" if they are.
# Makes use of .desktop file so we can right-click on packages adn choose to install them.
# GPL3, naturally :-)


if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
	dialog="Xdialog"
else
	dialog="dialog"
fi


if [ ! -e "$1" ]; then
	$dialog --title "Not Found" --msgbox "\n$1\nFile not found\n" 0 0
	exit
fi

fext=${1##*.}

if [ $fext != "tgz" ] && [ $fext != "txz" ]; then
	$dialog --title "Not a Package?" --msgbox "\n$1\nDoes not appear to be a valid software package\n" 0 0
	exit
fi

# make sure it IS a slackware installer package:
ret=`tar -tf $1 | grep install/slack-desc`
if [ ! $ret ]; then
	filename="${1##*/}"
	$dialog --title "Intall Package?" \
			--infobox "\n$filename\n\ndoes not apear to be\n  a Slackware Installer Package.  \n\n" 0 0 5000
	exit 0
fi  

# OK, this script is only accessible to root and installpkg
# will do it's usual package checks, so we can go ahead and call 
# installpkg, and run it in a term so folks can see what's doing...
if [[ $EUID -ne 0 ]]; then
  ktsuss xterm -e installpkg $1
else
  xterm -e installpkg $1
fi

if [ $? = 1 ]; then
	clear
	$dialog --title "Error..." \
        	--msgbox "$1\nwas not properly installed.\n\n" 0 0
else
	$dialog --title "Complete" \
        	--infobox "$1\nhas been installed.\n" 0 0 4000
fi
exit
